package com.demo.springBoot2.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.springBoot2.entity.dto.CampusRingtoneDTO;
import com.demo.springBoot2.entity.vo.CampusRingtoneVO;
import com.demo.springBoot2.result.MyPage;
import com.demo.springBoot2.result.Result;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.demo.springBoot2.service.ICampusRingtoneService;
import com.demo.springBoot2.entity.CampusRingtone;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 校园铃声 前端控制器
 * </p>
 *
 * @author wuHua·pl
 * @since 2024-04-11
 */
@Api(tags = "校园铃声")
@RestController
@RequestMapping("/campus-ringtone")
public class CampusRingtoneController {

    @Resource
    private ICampusRingtoneService campusRingtoneService;

    /**
     * 查询所有接口
     *
     * @return
     */
    @ApiOperationSupport(author = "wuHua·pl")
    @ApiOperation(value = "查询所有接口", protocols = "查询校园铃声所有数据")
    @GetMapping
    public Result<List<CampusRingtone>> findAll() {
        return Result.success(campusRingtoneService.list()).msg("查询所有成功");
    }

    /**
     * 根据id查询数据接口
     *
     * @param id
     * @return
     */
    @ApiOperationSupport(author = "wuHua·pl")
    @ApiOperation(value = "根据id查询数据接口", protocols = "通过组件id查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "组件id", required = true)
    })
    @GetMapping("/{id}")
    public Result<CampusRingtone> findOne(@PathVariable Integer id) {
        return Result.success(campusRingtoneService.getById(id)).msg("根据ID查询成功");
    }

    /**
     * 分页查询接口
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperationSupport(author = "wuHua·pl")
    @ApiOperation(value = "分页查询接口", protocols = "分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", example = "1", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页多少条", example = "10", required = true)
    })
    @GetMapping("/page")
    public Result<MyPage<CampusRingtone>> findPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        QueryWrapper<CampusRingtone> queryWrapper = new QueryWrapper<>();
        return Result.success().data(campusRingtoneService.page(new Page<>(pageNum, pageSize), queryWrapper)).msg("分页查询成功");
    }

    /**
     * 新增接口
     *
     * @param
     * @return
     */
    @ApiOperationSupport(author = "wuHua·pl")
    @ApiOperation(value = "新增接口")
    @PostMapping("/add")
// TODO 记得来创建这个VO
    public Result<String> add(@Valid @RequestBody CampusRingtoneVO campusRingtoneVO) {
        CampusRingtone campusRingtone = new CampusRingtone();
        BeanUtil.copyProperties(campusRingtoneVO, campusRingtone);
        campusRingtoneService.save(campusRingtone);
        return Result.success().msg("新增成功");
    }

    /**
     * 更新接口
     *
     * @param
     * @return
     */
    @ApiOperationSupport(author = "wuHua·pl")
    @ApiOperation(value = "更新接口")
    @PutMapping
    public Result<String> save(@RequestBody CampusRingtone campusRingtone) {
        campusRingtoneService.saveOrUpdate(campusRingtone);
        return Result.success().msg("更新成功");
    }

    /**
     * 删除接口
     *
     * @param id
     * @return
     */
    @ApiOperationSupport(author = "wuHua·pl")
    @ApiOperation(value = "删除接口", protocols = "通过id删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "组件id", required = true)
    })
    @DeleteMapping("/{id}")
    public Result<String> delete(@PathVariable Integer id) {
        campusRingtoneService.removeById(id);
        return Result.success().msg("批量删除成功");
    }

    /**
     * 批量删除接口
     *
     * @param ids
     * @return
     */
    @ApiOperationSupport(author = "wuHua·pl")
    @ApiOperation(value = "批量删除接口", protocols = "批量删除接口")
    @PostMapping("/del/batch")
    public Result<String> deleteBatch(@RequestBody List<Integer> ids) {
        campusRingtoneService.removeByIds(ids);
        return Result.success();
    }

    /**
     * 批量删除接口
     *
     * @param campusRingtoneVO
     * @return campusRingtoneDTO
     */
    @ApiOperationSupport(author = "wuHua·pl")
    @ApiOperation(value = "校园打铃时间查询", protocols = "校园打铃时间查询")
    @SaCheckRole("moral_agency")
    @PostMapping("/selectRingtoneByRingPeriod")
    public Result<CampusRingtoneDTO> selectRingtoneByRingPeriod(@Valid @RequestBody CampusRingtoneVO campusRingtoneVO) {
        CampusRingtone campusRingtone = new CampusRingtone();
        BeanUtil.copyProperties(campusRingtoneVO, campusRingtone);
        CampusRingtoneDTO campusRingtoneDTO = campusRingtoneService.selectRingtoneByRingPeriod(campusRingtone);
        return Result.success(campusRingtoneDTO).msg("查询成功");
    }
}